/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo
 * All rights reserved.
 */
package ac.jp.u_tokyo.SyncLib.language3;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import ac.jp.u_tokyo.SyncLib.language.Const;
import ac.jp.u_tokyo.SyncLib.language.EvaluationFailedException;
import ac.jp.u_tokyo.SyncLib.language.GraphCombinatorBody;
import ac.jp.u_tokyo.SyncLib.language.SyncBody;

public class SyncRefConstruct extends FixedParaSyncConstructBase
{
	ac.jp.u_tokyo.SyncLib.language.Program _targetProgram;
	public SyncRefConstruct(int line, ac.jp.u_tokyo.SyncLib.language.Program targetProgram)
	{
		super(line);
		_targetProgram = targetProgram;
	}

	private Map<String, Const> _genericArgs = new HashMap<String, Const>();
	private List<Var> _args = new LinkedList<Var>();
	private String _name;
	
	public void setName(String name) {
		_name = name;
	}

	public void addGenericArg(String name, Const value) {
		_genericArgs.put(name, value);
	}
	
	public void addArg(Var name) {
		_args.add(name);
	}


	@Override
	public Set<Var> getVarUsed(Collection<String> genericParas)
			throws EvaluationFailedException
	{
		for(Var v : _args) {
			v.check(genericParas);
		}
		return new HashSet<Var>(_args);
	}

	@Override
	public void addBody(GraphCombinatorBody body,
			Collection<String> genericParas) throws EvaluationFailedException
	{
		ac.jp.u_tokyo.SyncLib.language.SyncRef result = new ac.jp.u_tokyo.SyncLib.language.SyncRef(getLine(), _targetProgram, _name);
		for(Entry<String, Const> entry : _genericArgs.entrySet()) {
			result.addArg(entry.getKey(), entry.getValue());
		}
		List<Var> paras = new LinkedList<Var>(_args);
		body.addConnection(result, Helper.higherVar2LowerVar(paras));
		
		
	}
	
	
}
